﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 登录注册功能_sqlserver
{
    public partial class index : Form
    {
        public index(UserTModelForEF UF)
        {
            InitializeComponent();
            LoginNamelabel.Text = $"欢迎您{UF.Username}";
        }
        /// <summary>
        /// 初始化窗体时要求获取数据
        /// 并尝试把数据赋值给dataView控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void index_Load(object sender, EventArgs e)
        {
            UserdataGridView.AutoGenerateColumns = false;

            initData();
        }
        //更新数据库表格内容方法
        public void initData() {
            List<UserTModelForEF> UserList = new List<UserTModelForEF>();
            using (MyDbContext mydb = new MyDbContext())
            {
                UserList = mydb.UserTForWinform.ToList();
            }
            UserdataGridView.DataSource = UserList;
        }
        /// <summary>
        /// 编辑对应数据列 然后弹出编辑框 进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserdataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3) {
                //传递待修改的昵称
                string Nicheng = UserdataGridView.Rows[e.RowIndex].Cells["Nicheng"].Value.ToString();
                //传递唯一标识
                string username = UserdataGridView.Rows[e.RowIndex].Cells["Username"].Value.ToString();
                BianjiKuang bj = new BianjiKuang(Nicheng,username);
                bj.OnFormClosed += RefreshData; // 绑定子窗口关闭事件
                bj.ShowDialog();

            }
        }

        // 刷新父窗口数据的方法
        private void RefreshData()
        {
            initData();
        }

        private void UserdataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
